import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Confirm from '#cpt/temp/Confirm'
import Controls from '#cpt/temp/Controls'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
const TableComponent 		= $Lazy.state(()=>import('./cpt/table-component'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
const $time 				= $Lazy.file(()=>import('@com/utils/time'))
const BatchEditModal 		= $Lazy.state(()=>import('./cpt/batch-edit-modal'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label: '账期',				name: 'bill_phase_ids', 			type:'select', width:160,	loading:true, mode:'multiple' },
			{ label: '标本接收时间',		names: ['start_at','end_at'], 		name:'jz-time',	type: 'date-range', format:1 },
			{ label: '账单创建日期',		names: ['bill_start_at','bill_end_at'], name:'time-null', type:'date-range' },
			{ label: '区域名称',			name: 'region_id', 					type: 'select', 	loading:true },
			{ label: '客户名称',			name: 'hosp_id', 					type: 'select', 	loading:true },
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.getSelect()
		// 设置时间默认值
		$time.then(f=>{
			const arr = f.duration['1-today']()
			this.resetModel = { start_at: arr[0], end_at: arr[1], 'jz-time':arr }
			this.model = $fn.copy(this.resetModel)
			if(window.isInit){
				this.fetch()
			}
		})
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 账期
			$fn.setRow(forms, 'bill_phase_ids', await f.getCache({ api:'billPhaseSelect' } ) ) 
			// 客户名称
			$fn.setRow(forms, 'hosp_id', await f.getCache({ api:'hospital' } ) ) 
			// 区域名称
			$fn.setRow(forms, 'region_id', await f.getCache({ api:'bs_area' } ) ) 
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'bill-hosp/wait',{isIndex:true})
	
	// 表单
	cols = [
		{ title: '区域名称',		dataIndex: 'region_name', 	width:150 },
		{ title: '客户名称', 	dataIndex: 'hosp_name', 	width:200 },
		{ title: '账期', 		dataIndex: 'phase_name', 	width:150 },
		{ title: '标本数量', 	dataIndex: 'num', 			...$fn.table.num },
		{ title: '标准总价', 	dataIndex: 'sum_sprice', 	...$fn.table.num },
		{ title: '实际总价', 	dataIndex: 'sum_price', 	...$fn.table.num},
		// 新增初始结算价字段 20211018 耿琰
		{ title: '初始结算价',			dataIndex: 'sum_initial_price', 			...$fn.table.num },
	]
	// 操作按钮
	controls = () => {
		const rows = this.state.selectedRows[this.state.selectedRows.length-1]
		const param = $fn.hasObject(rows) ? {hosp_id: rows.hosp_id, bill_phase_id:rows.bill_phase_id} : {}
		return [
			// 增加批量修改 2022-4-21  --马婷 
			Controls.click.call(this,'批量修改',()=>this.batchEditRef.open(param) ),
			Confirm.call(this, '批量结算', 'bill-hosp/settlement',{ isKeys:true, clear:false, param:(keys,rows)=>{
				return { 
					param: rows.map(({hosp_id, bill_phase_id}) => ({hosp_id, bill_phase_id})),
				}
			} } ),
		]
	}
	render(){
		return (
			<>
				<Page title='账单结算' nobc controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this, { timeStr:'1-today' }) }
					{/* 表格 */}
					<div className='bcf fv ex'>
						{ Table.call(this,{ split:true, isIndex: true, isInit: window.isInit,
							onRow:( rows ) => {
								const jzTime = this.searchFormRef.getName('jz-time')
								const timeNull = this.searchFormRef.getName('time-null')
								const times = {
									start_at: jzTime[0],
									end_at: jzTime[1],
									bill_start_at: timeNull ? timeNull[0] : '',
									bill_end_at: timeNull ? timeNull[1] : ''
								}
								this.tableRef.load({...rows, ...times })
							}
						 }) }
					</div>
					{/* 表格 */}
					<div className='bcf fv mt5 ex pt10'>
						<TableComponent onRef={ ref => this.tableRef = ref }/>
					</div>
					<BatchEditModal onRef={ref=>this.batchEditRef=ref } />
				</Page>
			</>
		)
	}
}

export default Index